{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tkinter as tk\n",
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('500x500')\n",
    "var = tk.StringVar()\n",
    "l = tk.Label(window, textvariable = var, bg = 'green',width=40,height=10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "l.pack()\n",
    "on_hit = False\n",
    "def hit_me():\n",
    "    global on_hit\n",
    "    if on_hit == False:\n",
    "        on_hit = True\n",
    "        var.set('you hit me')\n",
    "    else:\n",
    "        on_hit=False\n",
    "        var.set('')\n",
    "b = tk.Button(window,text = 'hit me',width = 40, height = 10 ,command =hit_me)\n",
    "b.pack()\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tkinter as tk\n",
    "\n",
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "\n",
    "\n",
    "window.geometry('500x400')\n",
    "\n",
    "def insert_point():\n",
    "    var = e.get()\n",
    "    t.insert('insert',var)\n",
    "def insert_end():\n",
    "    var = e.get()\n",
    "    t.insert('end',var)\n",
    "e = tk.Entry(window)\n",
    "e.pack()\n",
    "b1 = tk.Button(window, text = 'insert point',width = 15,height = 2,command = insert_point)\n",
    "b1.pack()\n",
    "b2 = tk.Button(window,text = 'insert end',width = 15,height = 2,command = insert_end)\n",
    "b2.pack()\n",
    "t = tk.Text(window, height = 20)\n",
    "t.pack()\n",
    "\n",
    "window.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('Listbox')\n",
    "window.geometry('500x400')\n",
    "var1 = tk.StringVar()\n",
    "l = tk.Label(window,bg = 'yellow',width = 20,textvariable = var1)\n",
    "l.pack()\n",
    "def print_selection():\n",
    "    va = ib.get(ib.curselection())\n",
    "    var1.set(va)\n",
    "b1 = tk.Button(window,text = 'print selection',height = 2,width =15,command = print_selection)\n",
    "b1.pack()\n",
    "var2 = tk.StringVar()\n",
    "var2.set([11,22,33,44])\n",
    "ib = tk.Listbox(window, listvariable = var2)\n",
    "list_items = [1,2,3,4]\n",
    "for i in list_items:\n",
    "    ib.insert('end',i)\n",
    "ib.pack()\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('radiobutton')\n",
    "window.geometry('500x400')\n",
    "var = tk.StringVar()\n",
    "l = tk.Label(window, text = 'empty', width = 20,height=2)\n",
    "l.pack()\n",
    "def print_i():\n",
    "    l.config(text = 'you have selected'+var.get())\n",
    "se1 = tk.Radiobutton(window, text = 'selection A', variable = var, value = 'A',command = print_i)\n",
    "se1.pack()\n",
    "se2 = tk.Radiobutton(window, text = 'selection B', variable = var, value = 'B',command = print_i)\n",
    "se2.pack()\n",
    "se3 = tk.Radiobutton(window, text = 'selection C', variable = var, value = 'C',command = print_i)\n",
    "se3.pack()\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('Scale')\n",
    "window.geometry('500x400')\n",
    "var = tk.StringVar()\n",
    "l = tk.Label(window, text = 'empty', width = 30,height=2)\n",
    "l.pack()\n",
    "def print_i(v):\n",
    "    l.config(text = 'you have selected'+v)\n",
    "s = tk.Scale(window, label = 'try me',from_=5,to=11,orient = tk.HORIZONTAL,length = 400,showvalue=0, tickinterval = 3 ,resolution = 0.01,command = print_i)\n",
    "s.pack()\n",
    "window.mainloop() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('Scale')\n",
    "window.geometry('500x400')\n",
    "l = tk.Label(window, text = 'empty', width = 30,height=2)\n",
    "l.pack()\n",
    "\n",
    "\n",
    "def print_i():\n",
    "    if (var1.get() == 1) & (var2.get() == 0):   #如果选中第一个选项，未选中第二个选项\n",
    "        l.config(text='I love only Python ')\n",
    "    elif (var1.get() == 0) & (var2.get() == 1): #如果选中第二个选项，未选中第一个选项\n",
    "        l.config(text='I love only C++')\n",
    "    elif (var1.get() == 0) & (var2.get() == 0):  #如果两个选项都未选中\n",
    "        l.config(text='I do not love either')\n",
    "    else:\n",
    "        l.config(text='i love both')             #如果两个选项都选中\n",
    "var1 = tk.IntVar()\n",
    "var2 = tk.IntVar()\n",
    "s1 = tk.Checkbutton(window,variable = var1, text = 'python',onvalue = 1,offvalue = 0,\n",
    "                   command = print_i)\n",
    "s2 = tk.Checkbutton(window,variable = var2, text = 'c++',onvalue = 1,offvalue = 0,\n",
    "                   command = print_i)\n",
    "s1.pack()\n",
    "s2.pack()\n",
    "window.mainloop() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('Canvas')\n",
    "window.geometry('500x400')\n",
    "canvas = tk.Canvas(window,bg = 'blue', height = 200,width = 500)\n",
    "image_file = tk.PhotoImage(file='/home/shihao-z/图片/aff6e44747a6912cbd6e24cec5630484.gif')\n",
    "x0,y0,x1,y1 = 300,20,200,100\n",
    "image = canvas.create_image(10,10,anchor= 'nw', image = image_file)\n",
    "line = canvas.create_line(50,50,100,100)\n",
    "arcl = canvas.create_arc(50,50,100,100,start=0,extent=180)\n",
    "arc2 = canvas.create_arc(40,40,100,100,start=0,extent=180)\n",
    "oval = canvas.create_oval(40,40,100,100)\n",
    "rec = canvas.create_rectangle(x0,y0,x1,y1)\n",
    "canvas.pack()\n",
    "def movierec():\n",
    "    canvas.move(rec,0,2)\n",
    "s = tk.Button(window, text = 'move',height = 2,width=100, command = movierec)\n",
    "s.pack()\n",
    "window.mainloop() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-15-b312b17d1f4a>, line 19)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-15-b312b17d1f4a>\"\u001b[0;36m, line \u001b[0;32m19\u001b[0m\n\u001b[0;31m    window.config(menu=menubar)\u001b[0m\n\u001b[0m         ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('200x200')\n",
    "l = tk.Label(window,text='',bg = 'yellow')\n",
    "l.pack()\n",
    "counter = 0\n",
    "def do_job():\n",
    "    global counter\n",
    "    l.config(text = 'do ' + str(counter))\n",
    "    counter += 1\n",
    "menubar = tk.Menu(window)\n",
    "filemenu = tk.Menu(menubar, tearoff = 0)\n",
    "menubar.add_cascade(label = 'File',menu = filemenu)\n",
    "filemenu.add_command(label = 'new',command = do_job)\n",
    "filemenu.add_command(label = 'open',command = do_job)\n",
    "filemenu.add_command(label = 'save',command = do_job)\n",
    "filemenu.add_separator()\n",
    "filemenu.add_command(label = 'Exit', command=window.quit \n",
    "window.config(menu=menubar)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('200x200')\n",
    "l = tk.Label(window,text='',bg = 'yellow')\n",
    "l.pack()\n",
    "counter = 0\n",
    "def do_job():\n",
    "    global counter\n",
    "    l.config(text = 'do ' + str(counter))\n",
    "    counter += 1\n",
    "##创建一个菜单栏，这里我们可以把他理解成一个容器，在窗口的上方\n",
    "menubar = tk.Menu(window)\n",
    "\n",
    "##定义一个空菜单单元\n",
    "filemenu = tk.Menu(menubar, tearoff=0)\n",
    "\n",
    "##将上面定义的空菜单命名为`File`，放在菜单栏中，就是装入那个容器中\n",
    "menubar.add_cascade(label='File', menu=filemenu)\n",
    "\n",
    "##在`File`中加入`New`的小菜单，即我们平时看到的下拉菜单，每一个小菜单对应命令操作。\n",
    "##如果点击这些单元, 就会触发`do_job`的功能\n",
    "filemenu.add_command(label='New', command=do_job)\n",
    "filemenu.add_command(label='Open', command=do_job)##同样的在`File`中加入`Open`小菜单\n",
    "filemenu.add_command(label='Save', command=do_job)##同样的在`File`中加入`Save`小菜单\n",
    "\n",
    "filemenu.add_separator()##这里就是一条分割线\n",
    "\n",
    "##同样的在`File`中加入`Exit`小菜单,此处对应命令为`window.quit`\n",
    "filemenu.add_command(label='Exit', command=window.quit)\n",
    "\n",
    "\n",
    "submenu = tk.Menu(filemenu)##和上面定义菜单一样，不过此处实在`File`上创建一个空的菜单\n",
    "filemenu.add_cascade(label='Import', menu=submenu, underline=0)##给放入的菜单`submenu`命名为`Import`\n",
    "submenu.add_command(label=\"Submenu1\", command=do_job)##这里和上面也一样，在`Import`中加入一个小菜单命令`Submenu1`\n",
    "\n",
    "\n",
    "editmenu = tk.Menu(menubar, tearoff=0)\n",
    "menubar.add_cascade(label='edit',menu = editmenu)\n",
    "\n",
    "\n",
    "editmenu.add_command(label='cut', command=do_job)\n",
    "editmenu.add_command(label='copy', command=do_job)##同样的在`File`中加入`Open`小菜单\n",
    "editmenu.add_command(label='paste', command=do_job)##同样的在`File`中加入`Save`小菜单\n",
    "\n",
    "editmenu.add_separator()##这里就是一条分割线\n",
    "\n",
    "\n",
    "editmenu.add_command(label='exit',command=window.quit)\n",
    "editmenu.add_cascade(label='text',menu=submenu)\n",
    "\n",
    "\n",
    "window.config(menu=menubar)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('200x200')\n",
    "tk.Label(window, text = 'on the window').pack()\n",
    "\n",
    "frm = tk.Frame(window)\n",
    "frm.pack()\n",
    "frm_l = tk.Frame(frm,)\n",
    "frm_r = tk.Frame(frm)\n",
    "frm_l.pack(side='left')\n",
    "frm_r.pack(side='right')\n",
    "tk.Label(frm_l, text = 'on the window').pack()\n",
    "tk.Label(frm_r, text = 'on the window').pack()\n",
    "tk.Label(frm_l, text = 'on the window').pack()\n",
    "\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tkinter import messagebox\n",
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('200x200')\n",
    "def hit_me():\n",
    "    #tk.messagebox.showwarning(title='HI',message='hahahha')\n",
    "    \n",
    "    #tk.messagebox.showerror(title='HI',message='hahahha')\n",
    "    #tk.messagebox.askquestion(title='hi', message='hahah')\n",
    "    #tk.messagebox.askyesno(title='hi', message='hahah')\n",
    "    tk.messagebox.askokcancel(title='hi', message='hahah')\n",
    "    \n",
    "    #tk.messagebox.showinfo(title='HI',message='hahahha')\n",
    "tk.Button(window, text='hit me', command=hit_me).pack()\n",
    "window.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('200x200')\n",
    "tk.Label(window, text=1).pack(side='top')\n",
    "tk.Label(window, text=1).pack(side='bottom')\n",
    "tk.Label(window, text=1).pack(side='left')\n",
    "tk.Label(window, text=1).pack(side='right')\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('200x200')\n",
    "tk.Label(window, text=1).place(x=10,y=100)\n",
    "window.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "ename": "TclError",
     "evalue": "image \"pyimage11\" doesn't exist",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTclError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-40-15b8694bfab5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0ml\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtk\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCanvas\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwindow\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mheight\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mwidth\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m500\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mimage_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtk\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPhotoImage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfile\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'/home/shihao-z/下载/0304_1.png'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0manchor\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'nw'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mimage_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m \u001b[0mwindow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/tkinter/__init__.py\u001b[0m in \u001b[0;36mcreate_image\u001b[0;34m(self, *args, **kw)\u001b[0m\n\u001b[1;32m   2481\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mcreate_image\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2482\u001b[0m         \u001b[0;34m\"\"\"Create image item with coordinates x1,y1.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2483\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_create\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'image'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2484\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mcreate_line\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2485\u001b[0m         \u001b[0;34m\"\"\"Create line with coordinates x1,y1,...,xn,yn.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.6/tkinter/__init__.py\u001b[0m in \u001b[0;36m_create\u001b[0;34m(self, itemType, args, kw)\u001b[0m\n\u001b[1;32m   2472\u001b[0m         return self.tk.getint(self.tk.call(\n\u001b[1;32m   2473\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_w\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'create'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mitemType\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2474\u001b[0;31m             *(args + self._options(cnf, kw))))\n\u001b[0m\u001b[1;32m   2475\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mcreate_arc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2476\u001b[0m         \u001b[0;34m\"\"\"Create arc shaped region with coordinates x1,y1,x2,y2.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTclError\u001b[0m: image \"pyimage11\" doesn't exist"
     ]
    }
   ],
   "source": [
    "window = tk.Tk()\n",
    "window.title('my window')\n",
    "window.geometry('500x400')\n",
    "l = tk.Canvas(window,height=200,width=)\n",
    "image_file = tk.PhotoImage(file = '/home/shihao-z/下载/ea648a5560b0ba5ab130fa4c1144dd3e.gif')\n",
    "image = l.create_image(10,10,anchor='nw',image=image_file)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
